package com.wx.adapter.http.wx.ma.service;

import com.alibaba.fastjson2.JSON;
import com.wx.adapter.http.wx.ma.MiniProgramUserFeign;
import com.wx.adapter.http.wx.ma.domain.dto.MiniProgramLoginTokenDTO;
import com.wx.adapter.http.wx.ma.domain.dto.SnsJsCode2sessionDTO;
import com.wx.adapter.http.wx.ma.domain.req.StableTokenReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * wx_back_end
 *
 * @Auther: cheng.tang
 * @Date: 2023/2/12 6:44 PM
 * @Description:
 */
@Service
@Slf4j
public class MiniProgramServiceImpl implements MiniProgramService {

    @Value("${mini.program.appid}")
    private String appid;

    @Value("${mini.program.secret}")
    private String secret;

    @Autowired
    private MiniProgramUserFeign miniProgramUserFeign;

    @Override
    public SnsJsCode2sessionDTO snsJscode2session(
            String appid,
            String secret,
            String js_code,
            String grant_type) {
        String result = miniProgramUserFeign.snsJscode2session(appid, secret, js_code, grant_type);
        log.info("{}", result);
        return JSON.parseObject(result, SnsJsCode2sessionDTO.class);
    }

    @Override
    public SnsJsCode2sessionDTO snsJscode2session(String js_code) {
        String grant_type = "authorization_code";
        String result = miniProgramUserFeign.snsJscode2session(appid, secret, js_code, grant_type);
        log.info("{}", result);
        return JSON.parseObject(result, SnsJsCode2sessionDTO.class);
    }

    @Override
    public String getAccessToken() {
        String grant_type = "client_credential";
        StableTokenReq stableTokenReq = new StableTokenReq();
        stableTokenReq.setGrantType(grant_type);
        stableTokenReq.setAppid(appid);
        stableTokenReq.setSecret(secret);
        MiniProgramLoginTokenDTO miniProgramLoginTokenDTO = miniProgramUserFeign.getStableAccessToken(stableTokenReq);
        return miniProgramLoginTokenDTO.getAccessToken();
    }


}
